package cn.wolfcode.trip.admin.Controller;


import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQuery;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 攻略目录资源控制器
 */
@Controller
@RequestMapping("/strategyCatalog")
public class StrategyCatalogController {

    @Autowired
    private IStrategyCatalogService catalogService;

    @Autowired
    private IStrategyService strategyService;

    /**
     * 根据大攻略
     * 查询出所有的攻略
     * 含高级查询/分页
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String query(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model){
        qo.setOrderBy("sc.sequence");
        model.addAttribute("strategys",strategyService.listAll());
        PageInfo<StrategyCatalog> pageInfo= catalogService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/strategyCatalog/list";
    }

    /**
     * 保存或修改攻略目录
     * @param strategyCatalog
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(StrategyCatalog strategyCatalog){
        JSONResult jsonResult =new JSONResult();

        try {
            catalogService.saveOrUpdate(strategyCatalog);
        } catch (Exception e) {
            jsonResult.mark("操作失败,请稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }

    /**
     * 根据大攻略id查出所有的目录  不分页
     * @param strategyId
     * @return
     */
    @RequestMapping("/selectCatalogByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> selectCatalogByStrategyId(Long strategyId){
        return catalogService.selectCatalogByStrategyId(strategyId);
    }
}
